package cn.edu.gduf.luowowo.comment.service.impl;

import cn.edu.gduf.luowowo.comment.domain.ItemComment;
import cn.edu.gduf.luowowo.comment.repository.ItemCommentRepository;
import cn.edu.gduf.luowowo.comment.repository.QuestionCommentRepository;
import cn.edu.gduf.luowowo.comment.service.IItemCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ItemCommentServiceImpl implements IItemCommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ItemCommentRepository itemCommentRepository;

    @Override
    public String saveOrUpdate(ItemComment itemComment) {
        if ("has".equals(itemComment.getId()) ) {
            itemComment.setId(null);
        }else {
            ItemComment comment = itemCommentRepository.findById(itemComment.getId()).get();
            itemComment.setRefComment(comment);
        }
        itemComment.setCreateTime(new Date());
        itemCommentRepository.save(itemComment);
        return itemComment.getId();
    }

    @Override
    public List<ItemComment> findAll() {
        List<ItemComment> list = itemCommentRepository.findAll();
        return list;
    }
}
